//
//  MessageViewControler.m
//  HappyBug
//
//  Created by liulei on 2018/10/30.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "MessageViewControler.h"
#define APP_WITH  [UIScreen mainScreen].bounds.size.width
#define APP_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define BT_WITH  100

#import "MainScrollViewPage1.h"
#import "MainScrollViewPage2.h"
#import "MainScrollViewPage3.h"
#import "MainScrollViewPage4.h"
#import "MainScrollViewPage5.h"
#import "MainScrollViewPage6.h"
#import "MainScrollViewPage7.h"
@interface MessageViewControler ()

@end

@implementation MessageViewControler{
    
    UIScrollView *topscroll;
    UIScrollView *mainscroll;
    NSArray *titleArray;
    
    NSMutableArray *buttonArray;
    NSMutableArray *controllerArray;
    
    UIButton *txtbutton;
    CGFloat tottle;
    int buttonint;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self initTopView];
    [self initMainView];
    [self initData];
}
-(void)initMainView{
    
    mainscroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, topscroll.frame.origin.y+topscroll.frame.size.height,APP_WITH, APP_HEIGHT-topscroll.frame.origin.y-topscroll.frame.size.height)];
    mainscroll.bounces = NO;
    mainscroll.delegate = self;
    mainscroll.pagingEnabled = YES;
    mainscroll.showsHorizontalScrollIndicator = NO;
    mainscroll.tag=1000;
    mainscroll.showsVerticalScrollIndicator = YES;
    
    MainScrollViewPage1 *page1 =[[MainScrollViewPage1 alloc]init];
    MainScrollViewPage2 *page2 =[[MainScrollViewPage2 alloc]init];
    
    MainScrollViewPage3 *page3 =[[MainScrollViewPage3 alloc]init];
    
    MainScrollViewPage4 *page4 =[[MainScrollViewPage4 alloc]init];
    
    MainScrollViewPage5 *page5 =[[MainScrollViewPage5 alloc]init];
    
    MainScrollViewPage6 *page6 =[[MainScrollViewPage6 alloc]init];
    MainScrollViewPage7 *page7 =[[MainScrollViewPage7 alloc]init];
    
    controllerArray =[[NSMutableArray alloc]initWithObjects:page1,page2,page3,page4,nil];
    
    [mainscroll setContentSize:CGSizeMake(controllerArray.count *APP_WITH,0)];
    
    
    for(int i=0;i<controllerArray.count;i++){
        
        UIViewController *controller=controllerArray[i];
        controller.view.tag=10+i;
        controller.view.frame=CGRectMake(APP_WITH*i, 0, APP_WITH,APP_HEIGHT-topscroll.frame.origin.y-topscroll.frame.size.height);
        [mainscroll addSubview:controller.view];
    }
    
    [self.view addSubview:mainscroll];
    
    
}
-(void)initTopView{
    topscroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64,APP_WITH, 50)];
    topscroll.showsHorizontalScrollIndicator = NO;
    topscroll.showsVerticalScrollIndicator = NO;
    topscroll.delegate=self;
    topscroll.tag=100;

    topscroll.backgroundColor = [UIColor redColor];
    [self.view addSubview:topscroll];
    
    buttonArray=[[NSMutableArray array]init];
    
}

-(void)initData{
     
    titleArray =@[@"推荐",@"娱乐",@"新闻",@"科技"];
    CGRect txtframe=  [self rectWithText:titleArray.firstObject fontSize:12];
    for(int i=0;i<titleArray.count;i++){
        //每个字的高度
        CGRect txtframe2=  [self rectWithText:titleArray[i] fontSize:11];
        
        CGFloat bt_hei=txtframe.size.height;
        txtbutton=[[UIButton alloc]initWithFrame:CGRectMake(BT_WITH*i+20,(topscroll.frame.size.height-txtframe2.size.height)/2,txtframe2.size.width*2, bt_hei)];
        [txtbutton addTarget:self action:@selector(txtItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [txtbutton setTitle:titleArray[i] forState:UIControlStateNormal];
        txtbutton.tag=10+i;
        [txtbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [topscroll  addSubview:txtbutton];
        
       tottle=(txtbutton.frame.size.width+txtbutton.frame.origin.y*i);
        
        //第一个默认选中
        if(i==0){
            
            [txtbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        }
        [buttonArray addObject:txtbutton];
    }
    //能展示几个button
    buttonint =APP_WITH/tottle;
    [topscroll setContentSize:CGSizeMake(titleArray.count *BT_WITH,0)];
    
}

-(void)txtItemClick:(UIButton *)sender{
    
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sender.selected = YES;
    
    for(int i=0;i<buttonArray.count;i++){
        UIButton *bt =buttonArray[i];
        if(sender!=bt){
            [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }

    }
    
    
//    [self setMoveScrollViewContentOffsetWithCurrentClickView:sender andMoveScrollView:topscroll];

    [UIView animateWithDuration:0.5 animations:^{
        [mainscroll setContentOffset:CGPointMake((sender.tag - 10) * APP_WITH, 0)];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -------根据文本字体内容计算with height ---------
- (CGRect)rectWithText:(NSString *)text fontSize:(CGFloat)fontSize{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 3000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

{
    
    if(scrollView.tag==100){
        
          int buttomx= scrollView.contentOffset.x / APP_WITH;
        UIButton *bttton=   [buttonArray objectAtIndex:buttomx];
        [bttton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        for(int i=0;i<buttonArray.count;i++){
            UIButton *bt =buttonArray[i];
            if(bttton!=bt){
                [bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            }
        }
        [UIView animateWithDuration:0.5 animations:^{
            [mainscroll setContentOffset:CGPointMake(buttomx * APP_WITH, 0)];
        }];
        
    }else{
        
        int x= scrollView.contentOffset.x / APP_WITH;
        
        UIButton *bttton=   [buttonArray objectAtIndex:x];
        [bttton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        for(int i=0;i<buttonArray.count;i++){
            UIButton *bt =buttonArray[i];
            if(bttton!=bt){
                [bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            }
        }
       // [self setMoveScrollViewContentOffsetWithCurrentClickView:bttton andMoveScrollView:topscroll];
    }
   
}


-(void)setMoveScrollViewContentOffsetWithCurrentClickView:(UIView *)currentClickview andMoveScrollView:(UIScrollView *)moveScrollView
{
    if(moveScrollView.contentSize.width - moveScrollView.frame.size.width < 0)
        return;
    CGFloat x = currentClickview.frame.origin.x - (moveScrollView.frame.size.width - currentClickview.frame.size.width)/2;
    if(x > (moveScrollView.contentSize.width - moveScrollView.frame.size.width))
    {
        x = moveScrollView.contentSize.width - moveScrollView.frame.size.width;
    }
    else if(x < 0)
    {
        x = 0;
    }
    [moveScrollView setContentOffset:CGPointMake(x, currentClickview.frame.origin.y) animated:YES];
}
@end
